home *** CD-ROM | disk | FTP | other *** search
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- PARSE - Function
-
- Version 1.0
-
- for TURBO C & BORLANDC
-
- Reference Documentation
-
-
- Copyright (c) 1991
-
- Jonathan L. Rubin
- P.O. Box 548
- Newtown, PA
- 18940
-
- All Rights Reserved
-
-
-
-
-
-
-
-
-
- Table of Contents
-
-
- Disclaimer .......................................... 1
-
-
- Overview ............................................ 2
-
-
- Header Files ........................................ 3
-
-
- Function Call ....................................... 4
-
-
- Supported Functions ................................. 5
-
-
- Unknown Fields ...................................... 6
-
-
- PTEST.C ............................................. 7
-
- DISCLAIMER
-
-
- THE FUNCTIONS INCLUDED IN THIS PACKAGE ARE, TO THE BEST OF
- MY KNOWLEDGE, ORIGINAL OR USE STANDARD ACCEPTED ALGORITHMS.
- THIS SOFTWARE AND MATERIALS ARE SOLD (OR IN THE CASE OF
- SHAREWARE, PROVIDED FOR TRIAL) "AS IS" WITHOUT WARRANTY OF
- ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
- QUALITY AND PERFORMANCE OF THIS PRODUCT AND FUNCTIONS ARE
- WITH YOU. JONATHAN RUBIN DOES NOT WARRANT THAT THE
- FUNCTIONS CONTAINED IN THE PROVIDED CODE WILL MEET YOUR
- REQUIREMENTS OR THAT THE OPERATION OF THE FUNCTIONS OR
- PROGRAMS WILL BE UNINTERRUPTED OR ERROR FREE.
-
- OVERVIEW
-
-
- The PARSE function provides numerical solutions to numerical
- based equations. For example if a program were to pass the
- Parse function the following string:
-
- 100 * (5+2)
-
- The Parse function will return the value of 700 as a string;
-
- Example: 4+6/2
- will return 7 as a string.
-
- Example: (100*(5+2))/2
- will return 350 as a string.
-
- Example: (100*(5+2))/3
- will return 233.333333333333 as a string.
-
- Parse uses the following Operator Precedence when evaluating
- an expression.
-
- Operator Type Operators Associativity
- ----------------------------------------------------------
- Operators () Left to Right
- Multiplicative */ Left to Right
- Additive +- Left to Right
-
-
- Parse has a number of built in math functions for example
- when passed the following string.
-
- abs(3-7)
-
- The Parse function will return the value of 4 as a string;
-
-
- You may optionally place field names within a function for
- example:
-
- 100 * (sold/return)
-
- Parse will call a function located in your code and will
- pass you a string with the unknown fieldname upon which your
- function will replace the value of the unknown field with a
- numeric value. In this case the your function would be
- called twice, once with the string "sold" and again with the
- string "return". Parse will then carry out the computation
- with the new values.
-
- USING PARSE
-
-
- Header Files
- ------------
-
- Be sure to include the header file PARSE.H in your source
- code.
-
- Example when the PARSE.H file is in the working directory:
-
-
- #include "parse.h"
-
- Example when the PARSE.H file is in the include directory:
-
-
- #include <parse.h>
-
- Function Call
- -------------
-
-
- Parse(char *equation,char *result,int recursion);
- The third argument must be a zero.
-
- The mathematical symbols are supported:
-
- () : Operators
- + : Addition
- - : Subtraction
- * : Multiply
- / : Divide
-
- Included with this is a sample application which accepts an
- equation, as command line arguments, and prints the results.
-
- Example:
- --------
-
- #include <stdio.h>
- #include "parse.h"
-
- main()
- {
- char equation[25],result[25];
- int returncode;
-
- strcpy(equation,"25+(500/3)");
-
- if( (returncode = Parse(equation,result,0) < 0)
- printf("Error occurred %s\n",equation);
- else
- printf("Result of %s is %s\n",equation,result);
-
- exit(0);
- }
-
- ParseValue(char *field)
- {
- return(0);
- }
-
- Notes:
- ------
-
- You must include the function ParseValue in your code or
- your program will not compile. Further you must return a
- value greater than 0 if the Parse function is to complete
- successfully. For further information on the function
- ParseValue see section on unknown fields.
-
- Parse will return a value of < 0 if an error. Values of
- return codes are defined in the PARSE.H file.
-
- Supported Functions
- -------------------
-
-
- Your equations may include the following functions which are
- implemented as outlined in the Turbo C Library Manual. For
- complete information an each function please refer to that
- manual.
-
- abs()
- acos()
- asin()
- atan()
- ceil()
- cos()
- cosh()
- exp()
- fabs()
- floor()
- labs()
- log()
- log10()
- sin()
- sinh()
- sqrt()
- tan()
- tanh()
-
-
- Example: For the equation:
-
- acos(0.5)*2
-
- Parse will return 2.0943951023932
-
- Unknown fields
- --------------
-
-
- Parse will call the function ParseValue when it is unable to
- resolve the values of a token. This is extremely useful
- when you whish to build a program which contains stored
- field values which may be substituted in an mathematical
- equation. For example you may be building a spreadsheet
- program or other runtime package where the user might enter
- the following type of equation.
-
-
- 100*(price/cost)
-
-
- Parse will call the ParseValue function twice during the
- evaluation of the expression. The first time the Function
- will be called with a string value of 'price' and a second
- time with the value of 'cost'. Note: the ' is only used to
- delimit the field within this documentation and is not a
- part of the passed string.
-
- If a field is unknown then the function should return a -3
- negative value upon returning from the ParseValue function.
- This is defined in the PARSE.H file as FIELDUNKNOWN.
-
- A simplistic example follows:
-
-
- ParseValue(char *field)
- {
- if(strcmp(field,"ABC") == 0)
- strcpy(field,"123")
-
- else if(strcmp(field,"DEF") == 0)
- strcpy(field,"456")
-
- else
- return(FIELDUNKNOWN);
-
- return(0);
-
- }
-
- PTEST.C
- -------
-
-
- The following is the PTEST.C sample. PTEST takes an
- equation as part of the command line arguments and prints
- the results. A sample command line example is:
-
- C:\TC\TEST> PTEST 25+3
-
-
-
- /* PTEST.C */
-
-
- #include <stdlib.h>
- #include "parse.h"
-
- main(int argc,char *argv[])
- {
- int i,rc;
-
- char equ[80];
- char result[25];
-
- equ[0] = 0x0;
-
- for(i = 1; i <= argc ; i++)
- {
- strcat(equ,argv[i]);
- strcat(equ," ");
- }
-
- rc=Parse(equ,result,0);
-
- printf("rc=%d equation='%s' result=%s\n",rc,equ,result);
-
- return(0);
-
- }
-
-
- ParseValue(char *field)
- {
-
- return(0);
- }
-